<?php $this->pageHeader('Arbeidsmengde per medlem'); ?>
<a href="<?php echo $this->url(array('action' => 'index')) ?>" class="button">Tilbake</a>
<p class="clear"></p>


<?php 
    $this->filterForm->setAttrib('id', 'statistikk-hours-filter');
    $this->filterForm->setMethod('get');
    $this->filterForm->setAction($this->url());
    echo $this->filterForm;
?>

<table>
	<tr>
		<th><a href="javascript:void(0)" onclick="postFilterForm('<?php echo Core_Gateway_Statistics::HOURS_SORTBY_NAME ?>')" title="Sorter på navn">Navn</a></th>
		<th><a href="javascript:void(0)" onclick="postFilterForm('<?php echo Core_Gateway_Statistics::HOURS_SORTBY_SHIFTS ?>')" title="Sorter på antall vakter">Antall vakter</a></th>
		<th><a href="javascript:void(0)" onclick="postFilterForm('<?php echo Core_Gateway_Statistics::HOURS_SORTBY_HOURS ?>')" title="Sorter på antall timer">Antall timer</a></th>
	</tr>
    <?php
    $totalHours = 0;
	$totalShifts = 0;
	$totalParticipated = 0;
    foreach($this->hoursAndShifts as $row) :
        $name = $this->escape( $row['first_name'] . ' ' . $row['last_name']);
        $number_of_shifts = $row['number_of_shifts'];
        $hours = is_null($row['hours']) ? '0' : $row['hours'];
        $hours = (float)$hours;
		$totalShifts += $number_of_shifts;
        $totalHours += (float)$row['hours'];
        $totalParticipated++;
    ?>
        <tr>
            <td><a href="<?php echo $this->url(array("module"=>"core","controller"=>"user", "action"=>"view", "id"=>$row["user_id"])); ?>"><?php echo $name ?></a></td>
            <td><?php echo $number_of_shifts ?></td>
            <td><?php echo round($hours, 2); ?></td>
        </tr>
    <?php endforeach; ?>
	<tr>
		<th>Totalt</th>
		<th><?php echo $totalShifts ?></th>
		<th><?php echo round($totalHours, 2); ?></th>
	</tr>
	<?php if(!empty($totalParticipated)) { ?>
	<tr>
		<th>Per medlem</th>
		<th><?php echo round($totalShifts / $totalParticipated, 2); ?></th>
		<th><?php echo round($totalHours / $totalParticipated, 2); ?></th>
	</tr>
	<?php } ?>
</table>

<script language="javascript">
function postFilterForm(sortBy)
{
	var oldSortOrder = ($("#sortorder").val() == '<?php echo Core_Gateway_Statistics::ASC ?>') ? '<?php echo Core_Gateway_Statistics::ASC ?>' : '<?php echo Core_Gateway_Statistics::DESC ?>';
	var sortOrder = (oldSortOrder == '<?php echo Core_Gateway_Statistics::ASC ?>') ? '<?php echo Core_Gateway_Statistics::DESC ?>' : '<?php echo Core_Gateway_Statistics::ASC ?>';
	
	$("#sortorder").val(sortOrder);
	$("#sortby").val(sortBy);
	
	$("#statistikk-hours-filter")[0].submit();
}
</script>
